package com.wy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.entity.GoodsDeposit;
import com.wy.entity.GoodsEvaluation;
import com.wy.entity.GoodsInfo;
import com.wy.mapper.GoodsEvaluationMapper;
import com.wy.mapper.GoodsImageMapper;
import com.wy.service.GoodsEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:王渝
 * @Date: 2022/11/30  11:16
 * @Version 1.0
 */
@Service
public class GoodsEvaluationServiceImpl extends ServiceImpl<GoodsEvaluationMapper, GoodsEvaluation> implements GoodsEvaluationService {

    @Autowired
    private GoodsImageMapper goodsImageMapper;

    @Autowired
    private GoodsEvaluationMapper goodsEvaluationMapper;


    @Transactional(rollbackFor = {})
    @Override
    public boolean saveComment(GoodsEvaluation goodsEvaluation) {

        //保存评论信息
        boolean save = this.save(goodsEvaluation);
        if(!save){
            return false;
        }

        if(goodsEvaluation.getImages() != null && goodsEvaluation.getImages().length > 0){
            //保存图片
            int i = goodsImageMapper.insertBatchEvaluation(Arrays.asList(goodsEvaluation.getImages()), goodsEvaluation.getEvaluationId());
            if(i == 0){
                //回滚数据
                throw  new RuntimeException("评论失败");
            }
        }
        return true;
    }

    @Override
    public BigDecimal getAvgRateBySellerGoodsId(Long userId) {
        return goodsEvaluationMapper.getAvgRateBySellerGoodsId(userId);
    }

    @Override
    public BigDecimal getAvgRateBySellerUserId(Long userId) {
        return goodsEvaluationMapper.getAvgRateBySellerUserId(userId);
    }

    @Override
    public List<GoodsEvaluation> getAllBySellerUserId(Long sellerUserId) {
        return goodsEvaluationMapper.getAllBySellerUserId(sellerUserId);
    }

    @Override
    public PageInfo<GoodsEvaluation> getAllByBuyUserId(int currentPage, int pageSize, Long buyUserId) {
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsEvaluation> goodsInfos = goodsEvaluationMapper.getAllByBuyUserId(buyUserId);
        PageInfo<GoodsEvaluation> pageInfo = new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    @Override
    public  PageInfo<GoodsEvaluation> getAllByBuyUserId2(int currentPage, int pageSize, Long buyUserId) {
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsEvaluation> goodsInfos = goodsEvaluationMapper.getAllByBuyUserId2(buyUserId);
        PageInfo<GoodsEvaluation> pageInfo = new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<GoodsEvaluation> queryAllAdmin(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<GoodsEvaluation> goodsInfos = goodsEvaluationMapper.queryAllByAdmin();
        PageInfo<GoodsEvaluation> pageInfo = new PageInfo<>(goodsInfos);
        return pageInfo;
    }
}
